import utils from "../../utils/index";
import {
  rankModel,
  fansModel
} from "../../apis/index"
import {
  InterAd
} from '../../utils/InterAd'
import regeneratorRuntime from '../../libs/wxPromise.min';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signsIndex: 0,
    persons:0,
    totalPersonNum: 30, //签到总人数
    signs: [{
        start: 0,
        end: 11,
        name: '早安',
        hot: '10',
        idol: '10',
        is_sign: false,
        can_resign: false,
        img1: '/images/fensituan/qiandao_zaoan_sel.png',
        img2: '/images/fensituan/qiandao_zaoan_nor.png',
        img3: '/images/fensituan/qiandao_zao.png',
        field: 'm_sign_in',
        sFiled: 'm_sname', // 明星字段
        tFiled: 'm_name', // 粉丝团名称字段
        sname: '', //明星字段
        tname: '' // 粉丝团
      },
      {
        start: 11,
        end: 18,
        name: '午安',
        hot: '10',
        idol: '10',
        is_sign: false,
        can_resign: false,
        img1: '/images/fensituan/qiandao_wuan_sel.png',
        img2: '/images/fensituan/qiandao_wuan_nor.png',
        img3: '/images/fensituan/qiandao_zhong.png',
        field: 'n_sign_in',
        sFiled: 'n_sname', // 明星字段
        tFiled: 'n_name', // 粉丝团名称字段
        sname: '', //明星字段
        tname: '' // 粉丝团
      }, {
        start: 18,
        end: 24,
        name: '晚安',
        hot: '10',
        idol: '10',
        is_sign: false, //是否签到
        can_resign: false, //是否显示被签
        img1: '/images/fensituan/qiandao_wanan_sel.png',
        img2: '/images/fensituan/qiandao_wanan_nor.png',
        img3: '/images/fensituan/qiandao_wan.png',
        field: 'e_sign_in',
        sFiled: 'e_sname', // 明星字段
        tFiled: 'e_name', // 粉丝团名称字段
        sname: '', //明星字段
        tname: '' // 粉丝团
      }
    ]
  },
  mixins: [require('../../myMixin.js')],
  async onLoad(options) {
    let sid = options.sid ? options.sid : '' //粉丝团ID
    let fid = options.fid ? options.fid : '' //明星ID
    if (!sid) {
      utils.navigateBack({
        delta: 1
      })
      return
    }
    this.data.sid = sid
    this.data.fid = fid
    this.initSignInfo(sid, fid)
    this.signList()
    this.getShareInfo({type:3,sid:sid})
  },
  onReady: function () {
    let interAd = new InterAd('7fb828e435af61d59234ae3978c20221')
    interAd.showAd()
  },
  onShareAppMessage: function () {
    let {
      fid,
      sid
    } = this.data
    let {text,image} = this.data.share_data
    console.log(fid)
    console.log(sid)
    return {
      title: text,
      path: `pages/club/index?fid=${fid}&&sid=${sid}&&type=2`,
      imageUrl:image
    }
  },
  async initSignInfo(sid, fid) {
    utils.showLoading()
    let info = await rankModel.signInInfo({
      sid,
      fid
    })
    this.data.signInfo = info //服务器取回的签到信息
    let {
      signs,
      signsIndex
    } = this.data
    let now = new Date()
    let hour = now.getHours()
    let min = now.getMinutes()
    let _hour = `${hour}.${min}`
    signs.forEach((el, index) => {
      el.is_sign = info[el.field] != 0
      el.sname = info[el.sFiled]
      el.tname = info[el.tFiled]
      el.can_resign = el.end < _hour
      if (el.start < _hour && el.end >_hour) {
        signsIndex = index
      }
    });
    this.setData({
      signs: signs,
      signsIndex: signsIndex,
      persons: info.fuel,
      level:info.level
    },()=>{
      utils.hideAll()
    })
  },
  todaySign(e) {
    let index = e.currentTarget.dataset.index
    let {
      sid,
      fid,
      signs
    } = this.data
    let param = {
      sid,
      fid
    }
    utils.showLoading()
    signs.forEach((el, key) => {
      if (index == key) { //相等 表示该项签
        param[el.field] = '1'
      } else {
        param[el.field] = '0'
      }
    })
    rankModel.todaySign(param).then(res => {
      this.initSignInfo(sid, fid)
      utils.hideAll()
      this.setData({
        modalName: 'signModal'
      },()=>{
        this.signList()
      })
    })
  },
  hideModal() {
    this.setData({
      modalName: ''
    })
  },
  signLaunch() {
    let {
      fid,sid
    } = this.data

    fansModel.signLaunch({
      fid
    }).then(res => {
      this.initSignInfo(sid, fid)
      this.signList()
      this.setData({
        launching: true
      }, () => {
        setTimeout(() => {
          this.setData({
            launching: false
          })
        }, 1000)
      })
    })
  },
  signList() {
    let {
      fid
    } = this.data
    fansModel.signList({
      fid
    }).then(res => {
      this.setData({
        signList: res.arr
      })
    })
  }
})